/*
 * Copyright (c) 2014 jbing.org
 */
package jbing.log;

import java.util.Properties;

/**
 * jbing.log.Log
 * 
 * @author 旧城(jbing.org@gmail.com)
 * @date 2014/03/19
 * @version 0.0.2
 */
public abstract class AbstractLog implements Log {
	protected Class<?> clazz;
	protected Properties prop;
	protected boolean isConsole;
	protected boolean isFile;
	protected String filePath;
	protected String format;
	protected boolean isFatal;
	protected boolean isError;
	protected boolean isWarn;
	protected boolean isInfo;
	protected boolean isDebug;

	protected void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}

	protected void setProp(Properties prop) {
		this.prop = prop;
		initField();
	}

	private void initField() {
		String out = prop.getProperty("jbing.log.out");
		if (out != null && out.trim().equalsIgnoreCase("jbing.log.file")) {
			isFile = true;
		} else {
			isConsole = true;
		}
		String filepath = prop.getProperty("jbing.log.filepath");
		if (filepath == null || (filepath = filepath.trim()).length() <= 0) {
			filePath = "./jlog.log";
		} else {
			filePath = filepath;
		}
		String format2 = prop.getProperty("jbing.log.format");
		if (format2 == null || (format2 = format2.trim()).length() <= 0) {
			format = "%s %s[line=%s] %s";
		} else {
			format = format2;
		}

		String trace = prop.getProperty("jbing.log.trace");
		if (trace != null && (trace = trace.trim()).length() > 0) {
			String[] traces = trace.split(",");
			for (int i = 0; i < traces.length; i++) {
				String t = traces[i];
				t = t.trim();
				if (t.equalsIgnoreCase("debug")) {
					this.isDebug = true;
				} else if (t.equalsIgnoreCase("info")) {
					this.isInfo = true;
				} else if (t.equalsIgnoreCase("warn")) {
					this.isWarn = true;
				} else if (t.equalsIgnoreCase("error")) {
					this.isError = true;
				} else if (t.equalsIgnoreCase("fatal")) {
					this.isFatal = true;
				}
			}
		} else {
			isDebug = true;
		}
	}
}
